package com.xiguaji.tiktok.task.blogger.event;

import com.google.common.eventbus.AllowConcurrentEvents;
import com.google.common.eventbus.Subscribe;
import com.gugee.tiktok.common.model.dal.blogger.BloggerLogRepository;
import com.gugee.tiktok.common.model.dal.blogger.BloggerRepository;
import com.gugee.tiktok.common.model.dto.SlimBloggerDto;
import com.gugee.tiktok.common.model.mysql.BloggerLog;
import com.xiguaji.boot.eventbus.Event;
import com.xiguaji.boot.eventbus.EventBusTemplate;
import com.xiguaji.boot.eventbus.IEventListener;
import com.xiguaji.tiktok.task.aweme.event.SaveBloggerAwemeEventListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Component
public class FetchBloggerAwemeEventListener implements IEventListener<FetchBloggerAwemeEventListener.FetchBloggerAwemeEvent> {

    @Autowired
    BloggerLogRepository bloggerLogRepository;

    @Autowired
    BloggerRepository bloggerRepository;

    @Autowired
    @Lazy
    EventBusTemplate eventBusTemplate;

    @Override
    @Subscribe
    @AllowConcurrentEvents

    public void handle(FetchBloggerAwemeEvent event) {
        int bloggerLogType = event.resetWhenFetchNothing ?
                BloggerLog.BloggerLogType.FETCH_AWEME_LOOP.getValue() : BloggerLog.BloggerLogType.FETCH_AWEME.getValue();

        Optional<BloggerLog> bloggerLogOptional = bloggerLogRepository.findByLogType(bloggerLogType);
        BloggerLog bloggerLog;

        long id = 0;
        if (bloggerLogOptional.isPresent()) {
            id = bloggerLogOptional.get().getCurrentBloggerId();
            bloggerLog = bloggerLogOptional.get();
        } else {
            bloggerLog = new BloggerLog();
            bloggerLog.setLogType(bloggerLogType);
//            bloggerLog.setExtValue("");
            bloggerLog.setCreateBy(event.getSource());
            bloggerLog.setUpdateBy(event.getSource());
        }

        List<SlimBloggerDto> bloggers = bloggerRepository.findSlimBloggersByIdAndCount(id,200);
        if (CollectionUtils.isEmpty(bloggers)) {
            if (event.resetWhenFetchNothing) {
                bloggerLog.setCurrentBloggerId(0);
                bloggerLogRepository.save(bloggerLog);
            }
            return;
        }

        bloggerLog.setCurrentBloggerId(bloggers.get(bloggers.size() - 1).getId());
        bloggerLogRepository.save(bloggerLog);

        var saveBloggerAwemeEvent = new SaveBloggerAwemeEventListener.SaveBloggerAwemeEvent(
                bloggers.stream().map(a -> a.getUid()).collect(Collectors.toList())
        );

        eventBusTemplate.post(saveBloggerAwemeEvent);
    }

    public static class FetchBloggerAwemeEvent extends Event {

        public FetchBloggerAwemeEvent(boolean resetWhenFetchNothing) {
            super("FetchBloggerAweme");
            this.resetWhenFetchNothing = resetWhenFetchNothing;
        }

        private boolean resetWhenFetchNothing;
    }
}
